package com.yutel.phone.binh.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;

import com.yutel.phone.binh.R;
import com.yutel.phone.binh.comm.BitmapManager;
import com.yutel.phone.binh.data.vo.Video;
import com.yutel.phone.binh.service.ServiceReceiver;
import com.yutel.phone.binh.service.StirringService;
import com.yutel.phone.binh.service.SyncService;

public class StirringActivity extends BaseActivity implements
		OnItemClickListener, ServiceReceiver.Receiver {
	private static final String TAG = "StirringActivity";
	private ServiceReceiver mReceiver;
	private SimpleAdapter mAdapter;
	private ProgressBar pb;
	private ListView lv;
	private TextView tv;
	private String[] from = new String[] { "vid", "name", "info", "score",
			"poster", "update_info" };
	private int[] to = new int[] { R.id.vid, R.id.name, R.id.info, R.id.score,
			R.id.poster, R.id.update_info };
	private List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
	private BitmapManager bmpManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daybill_list);
		pb = (ProgressBar) findViewById(android.R.id.empty);
		lv = (ListView) findViewById(android.R.id.list);
		tv = (TextView) findViewById(R.id.emptyText);
		mReceiver = new ServiceReceiver(new Handler());
		mReceiver.setReceiver(this);
		bmpManager = new BitmapManager(BitmapFactory.decodeResource(
				getResources(), R.drawable.empty_img));
		mAdapter = new SimpleAdapter(this, fillMaps, R.layout.daybill_item,
				from, to);
		mAdapter.setViewBinder(viewBinder);
		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(this);
		// service
		Intent intent = new Intent(Intent.ACTION_SYNC, null, this,
				StirringService.class);
		intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, mReceiver);
		startService(intent);
	}

	private void updateView(Bundle resultData) {
		ArrayList<Video> mDataList = (ArrayList<Video>) resultData
				.getSerializable(StirringService.class.getSimpleName());
		if (mDataList != null && mDataList.size() > 0) {
			Log.i(TAG, "updateView=" + mDataList.size());
			for (Video item : mDataList) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("vid", item.getVid());
				map.put("poster", item.getPoster());
				map.put("name", item.getName());
				map.put("info", item.getVid());
				map.put("score", item.getScore());
				map.put("update_info", item.getQualityLevel());
				fillMaps.add(map);
			}
			mAdapter.notifyDataSetChanged();
			pb.setVisibility(View.GONE);
			lv.setVisibility(View.VISIBLE);
			tv.setVisibility(View.GONE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView tx = (TextView) arg1.findViewById(R.id.vid);
		Log.i(TAG, "arg2=" + arg2 + ",id=" + tx.getText());
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra(Intent.EXTRA_UID, tx.getText());
		startActivity(intent);
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		Log.i(TAG, "onReceiveResult=" + resultCode);
		switch (resultCode) {
		case SyncService.STATUS_RUNNING:
			pb.setVisibility(View.VISIBLE);
			lv.setVisibility(View.GONE);
			tv.setVisibility(View.GONE);
			break;
		case SyncService.STATUS_FINISHED:
			updateView(resultData);
			break;
		case SyncService.STATUS_ERROR:
			tv.setText(R.string.service_error);
			pb.setVisibility(View.GONE);
			lv.setVisibility(View.GONE);
			tv.setVisibility(View.VISIBLE);
			break;
		default:
			pb.setVisibility(View.GONE);
			Log.w(TAG, "warring resultCode=" + resultCode);
		}
	}

	private ViewBinder viewBinder = new ViewBinder() {
		@Override
		public boolean setViewValue(View view, Object data,
				String textRepresentation) {

			if (view != null && data != null) {
				switch (view.getId()) {
				case R.id.poster:
					if (data != null && view instanceof ImageView) {
						bmpManager
								.loadBitmap(data.toString(), (ImageView) view);
					}
					return true;
				case R.id.name:
					if (data != null && view instanceof TextView) {
						((TextView) view).setText(data.toString());
					}
					return true;
				case R.id.score:
					if (data != null && view instanceof TextView) {
						((TextView) view).setText(data.toString());
					}
					return true;
				case R.id.info:
					if (data != null && view instanceof TextView) {
						((TextView) view).setText(data.toString());
					}
					return true;
				case R.id.update_info:
					if (data != null && view instanceof TextView) {
						((TextView) view).setText(data.toString());
					}
					return true;
				}
			}
			return false;
		}
	};
}
